package com.novax.ex.activity.provider.service;

import com.novax.ex.activity.infrastructure.entity.TaskVersionConfig;
import com.novax.ex.activity.infrastructure.mapper.TaskVersionConfigMapper;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.enums.TaskTemplateEnum;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import com.novax.ex.activity.infrastructure.entity.TaskVersionConfig;
import com.novax.ex.activity.infrastructure.entity.TaskVersionInfo;
import com.novax.ex.activity.infrastructure.mapper.TaskVersionConfigMapper;
import com.novax.ex.activity.infrastructure.mapper.TaskVersionInfoMapper;
import com.novax.ex.activity.open.model.request.task.TaskVersionConfigRequest;
import com.novax.ex.common.base.BaseMapper;
import com.novax.ex.common.base.BaseService;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.ArrayList;

/**
 * @Description 版本配置 service
 * @Author ChenXi
 * @Date 2023/06/20/15:43
 */

@Service
public class TaskVersionConfigService extends BaseService {

    @Resource
    private TaskVersionConfigMapper taskVersionConfigMapper;

    @Resource
    private TaskVersionInfoMapper taskVersionInfoMapper;

    // 版本配置默认新手任务
    private static ArrayList<TaskVersionInfo> defaultTaskVersionInfos = new ArrayList<>();

    static {
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId(), 2, null, 1));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_ANY_TRANSACTION.getTaskId(), 3,1, null));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.FOLLOW_TWITTER.getTaskId(),4,null, null));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.JOIN_TELEGRAM.getTaskId(),5,null, null));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId(),6,0, 1));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_CONTRACT_TRANSACTION.getTaskId(),7,10, null));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_SPOT_TRANSACTION.getTaskId(),8,10, null));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId(),9,null,1 ));
        defaultTaskVersionInfos.add(new TaskVersionInfo(null, TaskTemplateEnum.COMPLETE_TOP_UP.getTaskId(),10,null, 1));
    }

    @Override
    protected BaseMapper getBaseMapper() {
        return taskVersionConfigMapper;
    }

    /**
     * 查询原始的版本
     *
     * @return 版本信息
     */
    public TaskVersionConfig findFirstVersion() {
        return taskVersionConfigMapper.selectFirstVersion();
    }

    /**
     * @Description: 添加版本配置
     * @Author: ChenXi
     * @Date:  2023/6/21 11:11
     * @param taskVersionConfigRequest
     * @return: com.novax.ex.common.results.ReturnResult<?>
     **/
    @Transactional(rollbackFor = Exception.class)
    public boolean addTaskVersionConfig(TaskVersionConfigRequest taskVersionConfigRequest) {
        int i = 0;
        TaskVersionConfig taskVersionConfig = CopyUtils.copyObject(taskVersionConfigRequest, TaskVersionConfig.class);
        i = i + taskVersionConfigMapper.insertSelective(taskVersionConfig);
        i = i + taskVersionInfoMapper.defaultTaskInsert(taskVersionConfig.getId(), defaultTaskVersionInfos);
        return i > 1;
    }

    /**
     * @Description: 删除
     * @Author: ChenXi
     * @Date:  2023/6/30 10:54
     * @param id
     * @return: boolean
     **/
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTask(Long id) {
        int i = 0;
        i = i + taskVersionConfigMapper.deleteByPrimaryKey(id);
        i = i + taskVersionInfoMapper.deleteByVersionId(id);
        return i > 1;
    }

}
